﻿using Iworld.Author.Model;
using Iworld.Basis.Help;
using Iworld.Basis.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Manager
{
    /// <summary>
    /// 用户的管理者对象
    /// </summary>
    [RegisterToManagerService]
    public class AuthorManager : ManagerBase<IModelToDbContextOfAuthor, AuthorManager.Actions, Iworld.Author.Model.Author>
    {

        /// <summary>
        /// 实例化一个新的用户的管理者对象
        /// </summary>
        /// <param name="db">数据库连接对象</param>
        public AuthorManager(IModelToDbContextOfAuthor db)
            : base(db)
        {

        }

       


        /// <summary>
        /// 用户登人
        /// </summary>
        /// <param name="package">用于登陆的数据集</param>
        /// <returns>返回用户的实例</returns>
        public Iworld.Author.Model.Author Login(IPackageForLogin package)
        {
            string account = VerifyHelper.EliminateSpaces(package.Account);
            string password = VerifyHelper.EliminateSpaces(package.Password);
            VerifyHelper.Check(account, VerifyHelper.Key.Nickname);
            VerifyHelper.Check(password, VerifyHelper.Key.Password);
            password = EncryptHelper.EncryptByMd5(password);
            Iworld.Author.Model.Author user = db.Authors.FirstOrDefault(x => x.Account == package.Account && x.Password == password);
            if (user == null)
            {
                throw new Exception("用户名或密码错误，请重新输入");
            }
            if (user.Status == AuthorStatus.禁止访问)
            {
                throw new Exception("黑名单，你懂的。");
            }
            
            OnExecuting(Actions.Login, user);
            user.OnLogin(package.Ip, package.Address);
            OnExecuted(Actions.Login, user);
            db.SaveChanges();

            return user;
        }

        /// <summary>
        /// 监听：用户的账户余额改变
        /// </summary>
        /// <param name="info">数据集</param>
        [Listen(typeof(GoldChangeRecordManager), GoldChangeRecordManager.Actions.Create, ExecutionOrder.After)]
        public static void MonitorGoldChange(InfoOfSendOnManagerService info)
        {
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            GoldChangeRecord model = (GoldChangeRecord)info.Model;
            Iworld.Author.Model.Author user = db.Authors.Find(model.Owner.Id);
            user.Golds += model.Sum;
        }

        /// <summary>
        /// 监听：冻结资金
        /// </summary>
        /// <param name="info">数据集</param>
        [Listen(typeof(WithdrawalsRecordManager), WithdrawalsRecordManager.Actions.Create, ExecutionOrder.After)]
        public static void Monitor_FreezeMoney(InfoOfSendOnManagerService info)
        {
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            WithdrawalsRecord model = (WithdrawalsRecord)info.Model;

            if (model.Status == WithdrawalsStatus.处理中)
            {
                Model.Author user = db.Authors.Find(model.Owner.Id);
                user.FrozenGolds += model.Golds;
            }
        }

        /// <summary>
        /// 监听：蒸发被冻结的资金
        /// </summary>
        /// <param name="info">数据集</param>
        [Listen(typeof(WithdrawalsRecordManager), WithdrawalsRecordManager.Actions.ChangeStatus, ExecutionOrder.After)]
        public static void Monitor_ThawMoney(InfoOfSendOnManagerService info)
        {
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            WithdrawalsRecord model = (WithdrawalsRecord)info.Model;

            if (model.Status == WithdrawalsStatus.失败 ||
                model.Status == WithdrawalsStatus.提现成功)
            {
                Model.Author user = db.Authors.Find(model.Owner.Id);
                user.FrozenGolds -= model.Golds;
            }
        }

        #region 枚举

        /// <summary>
        /// 动作
        /// </summary>
        public enum Actions
        {
            /// <summary>
            /// 创建新用户
            /// </summary>
            Create,
            /// <summary>
            /// 修改用户信息
            /// </summary>
            Update,
            /// <summary>
            /// 移除用户
            /// </summary>
            Remove,
            /// <summary>
            /// 用户登录
            /// </summary>
            Login,
            /// <summary>
            /// 赠送金币
            /// </summary>
            GiveGolds
        }
 

        #endregion

        #region 接口
        /// <summary>
        /// 定义用于登陆的数据集
        /// </summary>
        public interface IPackageForLogin
        {
            /// <summary>
            /// 账号
            /// </summary>
            string Account { get; }

            /// <summary>
            /// 密码
            /// </summary>
            string Password { get; }

            /// <summary>
            /// 网络地址
            /// </summary>
            string Ip { get; }

            /// <summary>
            /// 实际地址
            /// </summary>
            string Address { get; }
        }

     

 

        #endregion
       

    }
}
